const app = getApp()

Component({
  properties: {
    orderInfo: {
      type: Object,
      value: {}
    },
    callPay: {
      type: Boolean,
      value: false
    },
    contact: {
      type: Boolean,
      value: false
    }
  },
  data: {
    loading: false
  },
  ready() {
    let that = this
    setTimeout(function () {
      if (that.data.callPay) {
        that.unifiedOrder()
      }
    }, 1000)
  },
  methods: {
    orderReceive() {
      let that = this
      wx.showModal({
        content: '是否确认收货吗？',
        cancelText: '我再想想',
        confirmColor: '#ff0000',
        success(res) {
          if (res.confirm) {
            let id = that.data.orderInfo.id
            app.api.orderReceive(id)
              .then(res => {
                that.triggerEvent('orderReceive', res)
              })
          }
        }
      })
    },
    orderDel() {
      let that = this
      wx.showModal({
        content: '确认删除该订单吗？',
        cancelText: '我再想想',
        confirmColor: '#ff0000',
        success(res) {
          if (res.confirm) {
            let id = that.data.orderInfo.id
            app.api.orderDel(id)
              .then(res => {
                that.triggerEvent('orderDel', res)
              })
          }
        }
      })
    },
    unifiedOrder() {
      this.setData({
        loading: true
      })
      var that = this
      let orderInfo = this.data.orderInfo
      app.api.unifiedOrder({
          tradeType: 'JSAPI',
          id: orderInfo.id,
          paymentType: 3
        })
        .then(res => {``
          this.setData({
            loading: false
          })
          let payData = res.data
          if (!payData || orderInfo.paymentPrice <= 0) { //0元付款
            that.triggerEvent('unifiedOrder', res)
          } else {
            wx.requestPayment({
              'timeStamp': payData.timeStamp,
              'nonceStr': payData.nonceStr,
              'package': payData.packageValue,
              'signType': payData.signType,
              'paySign': payData.paySign,
              'success': function (res) {
                that.triggerEvent('unifiedOrder', res)
              },
              'fail': function (res) {},
              'complete': function (res) {
                console.log(res)
              }
            })
          }
        }).catch(() => {
          this.setData({
            loading: false
          })
        })
    },
    urgeOrder() {
      wx.showToast({
        title: '已提醒卖家发货',
        icon: 'success',
        duration: 2000
      })
    },
    orderLogistics() {
      wx.navigateTo({
        url: '/pages/order/order-logistics/index?id=' + this.data.orderInfo.orderLogistics.id
      })
    },
    orderAppraise() {
      wx.navigateTo({
        url: '/pages/appraises/form/index?orderId=' + this.data.orderInfo.id
      })
    },
    // 跳转到付款页
    toPay() {
      wx.navigateTo({
        url: '/pages/order/order-pay/index?sourcePage=createOrder&orderId=' + this.data.orderInfo.id
      });
    },
    handleContact() {
      wx.makePhoneCall({
        phoneNumber: '13811111111'
      })
    },
    // 师傅接单
    handleAcceptOrder() {
      let that = this
      wx.showModal({
        content: '是否确认接单？',
        cancelText: '我再想想',
        confirmColor: '#ff0000',
        success(res) {
          if (res.confirm) {
            let id = that.data.orderInfo.id
            app.api.providerAcceptOrder({
              id: id
            }).then(res => {})
          }
        }
      })
    },
    // 师傅取消订单
    orderCancel() {
      let that = this
    },
    // 师傅结单反馈
    orderComplete() {}
  }
})